package com.ruoyi.dynamic;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.datasource.DynamicDataSourceContextHolder;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.web.controller.module.mall.good.domain.MallGood;
import com.ruoyi.web.controller.module.mall.good.mapper.MallGoodMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;


/**
 * @Author alison
 * @Date 2024/12/14 1:00
 * @Version 1.0
 * @Description
 */
@Slf4j
@Service
public class DsService {
    @Autowired
    private SysUserMapper sysUserMapper;

//    @Transactional(value =  Transactional.TxType.REQUIRES_NEW)// 解决方法,重启新事务
    @DataSource(value = DataSourceType.SLAVE)
    public void querys() {
        System.out.println("--------3--------");
        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName());
        System.out.println("DynamicDataSourceContextHolder.getDataSourceType() = " + DynamicDataSourceContextHolder.getDataSourceType());
        SysUser user = sysUserMapper.selectUserByLoginName("sched");
        log.info("user: " + user);
    }
    @Autowired
    private MallGoodMapper goodMapper;
    @DataSource(value = DataSourceType.MASTER)
    public void insertM(){
        MallGood good = MallGood.builder().id(1).name("办公电脑").price(new BigDecimal(5000)).build();
        good.setCreateBy("admin");
        goodMapper.insertMallGood(good);
    }
    @DataSource(value = DataSourceType.SLAVE)
    public void insertS(){
        MallGood good = MallGood.builder().id(2).name("头戴耳机").price(new BigDecimal(200)).build();
        good.setCreateBy("admin");
        goodMapper.insertMallGood(good);
    }
}
